package com.lv.common.utils;

import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;

/**
 * Created by ruancw on 2018/4/2.
 */

public class DisplayUtils {

    public static Point getScreenMetrics(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        Log.i("RCW", "Width = " + screenWidth + " Height = " + screenHeight
                + " densityDpi = " + dm.densityDpi);
        return new Point(screenWidth, screenHeight);

    }


    public static Rect createCenterRect(Context context, Rect rect) {
        int xx = rect.right - rect.left;
        int yy = rect.bottom - rect.top;
//        int left = (getScreenMetrics(context).x - dp2px(context, xx)) / 2;
//        int top = (getScreenMetrics(context).y - dp2px(context, yy)) / 2;
//        int right = (getScreenMetrics(context).x + dp2px(context, xx)) / 2;
//        int bottom = (getScreenMetrics(context).y + dp2px(context, yy)) / 2;

        int left = rect.left;
        int top = rect.top;
        int right = rect.right;
        int bottom = rect.bottom;
//
        Log.e("RCW", xx + "@" + yy);
        Log.e("RCW", px2dp(context, xx) + "@" + px2dp(context, yy));
        Log.e("RCW", left + "@" + top + "@" + right + "@" + bottom);
        Log.e("RCW", px2dp(context, left) + "@" + px2dp(context, top) + "@" + px2dp(context, right) + "@" + px2dp(context, bottom));
        return new Rect(left, top, right, bottom);
    }

    public static Rect createCenterRect(Context context, int w, int h) {
        int w_ = w;
        int h_ = h;
        int wi = getScreenMetrics(context).x;
        int hi = getScreenMetrics(context).y;
        if (wi / hi > w / h) {
            h = hi - 200;
            w = w * h / h_;
        } else {
            w = wi - 200;
            h = h * w / w_;
        }
        int left = (getScreenMetrics(context).x - w) / 2;
        int top = (getScreenMetrics(context).y - h) / 2;
        int right = (getScreenMetrics(context).x + w) / 2;
        int bottom = (getScreenMetrics(context).y + h) / 2;
// int left = (getScreenMetrics(context).x - dp2px(context, w)) / 2;
//        int top = (getScreenMetrics(context).y - dp2px(context, h)) / 2;
//        int right = (getScreenMetrics(context).x + dp2px(context, w)) / 2;
//        int bottom = (getScreenMetrics(context).y + dp2px(context, h)) / 2;

//        int left = dp2px(context, rect.left);
//        int top = dp2px(context, rect.top);
//        int right = getScreenMetrics(context).x - dp2px(context, rect.right);
//        int bottom = getScreenMetrics(context).y - dp2px(context, rect.bottom);
//
        return new Rect(left, top, right, bottom);
    }

    /**
     * 将px装换成dp，保证尺寸不变
     *
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2dp(Context context, float pxValue) {
        float density = context.getResources().getDisplayMetrics().density;//得到设备的密度
        return (int) (pxValue / density + 0.5f);
    }

    public static int dp2px(Context context, float dpValue) {
        float density = context.getResources().getDisplayMetrics().density;
        Log.i("RCW", "density=" + density);
        return (int) (dpValue * density + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        float scaleDensity = context.getResources().getDisplayMetrics().scaledDensity;//缩放密度
        return (int) (pxValue / scaleDensity + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        float scaleDensity = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * scaleDensity + 0.5f);
    }
}
